#import "ALTemplateEditorColumnEntry.h"
#import "NSObject+BlockObservation.h"
#import "ALElementBarTextFieldCell.h"
#import "ALElementBarPopUpButtonCell.h"
#import "ALElementBarAddButtonCell.h"
#import "ALElementTemplate.h"


#pragma mark Constants
//static const NSUInteger ALTemplateEditorLabelTextFieldTag = 101;
//static const NSUInteger ALTemplateEditorTypePopupButtonTag = 102;
//static const NSUInteger ALTemplateEditorAddButtonTag = 103;
static const NSUInteger ALTemplateEditorInitialResponderTag = 120;
static const NSUInteger ALTemplateEditorLastResponderTag = 121;


#pragma mark -
@interface ALTemplateEditorColumnEntry (ALTemplateEditorColumnEntryPrivateMethods)
-(NSView *)detailViewForType_:(ALElementType)type;
@end


#pragma mark -
@implementation ALTemplateEditorColumnEntry

//@synthesize elementDetailViews;
@synthesize currentDetailView;
@synthesize textDetailView;
@synthesize ratingDetailView;
@synthesize elementLabel;
@synthesize elementTypePopUp;
@synthesize elementAddButton;


//-(id)copyWithZone:(NSZone *)zone {
//	ALTemplateEditorColumnEntry *copy = [super copyWithZone:zone];
//	[copy setElementDetailViews:[self elementDetailViews]];
//	return copy;
//}

-(void)columnEntryWasAddedToColumnView {
	[super columnEntryWasAddedToColumnView];
	AMBlockTask selectionTask = ^(id object, NSDictionary *change) {
//		NSTextField *elementLabelTextField = (NSTextField *)[[self view] viewWithTag:ALTemplateEditorLabelTextFieldTag];
		ALElementBarTextFieldCell *elementLabelTextFieldCell = (ALElementBarTextFieldCell *)[[self elementLabel] cell];
		[elementLabelTextFieldCell setSelected:[self selected]];
		
//		NSControl *popupButton = (NSControl *)[[self view] viewWithTag:ALTemplateEditorTypePopupButtonTag];
		ALElementBarPopUpButtonCell *popupCell = (ALElementBarPopUpButtonCell *)[[self elementTypePopUp] cell];
		[popupCell setSelected:[self selected]];
		
//		NSControl *addButton = (NSControl *)[[self view] viewWithTag:ALTemplateEditorAddButtonTag];
		ALElementBarAddButtonCell *buttonCell = (ALElementBarAddButtonCell *)[[self elementAddButton] cell];
		[buttonCell setSelected:[self selected]];
	};
	[self addObserverForKeyPath:ALSelectedKey task:selectionTask];
}

-(void)columnEntryDidBeginEditingAtPoint:(NSPoint)clickPoint {
	[super columnEntryDidBeginEditingAtPoint:clickPoint];
//	NSInteger tag = ALElementDetailsViewBaseTag + [[[self representedObject] elementType] integerValue];
//	NSView *detailsView = [[self elementDetailViews] viewWithTag:tag];
	NSView *detailsView = [self detailViewForType_:(ALElementType)[[[self representedObject] elementType] integerValue]];
	CGFloat detailHeight = [detailsView bounds].size.height;
	NSRect viewBounds = [[self view] bounds];
	NSRect detailFrame = viewBounds;
	detailFrame.origin.y = 1;  // Element bar is at y = 1, so this positions the details view immediately under the bar
	detailFrame.size.height = 0;
	NSView *detailsClone = [self cloneAndAdoptSubview:detailsView];
	[detailsClone setFrame:detailFrame];
	[self setCurrentDetailView:detailsClone];
	[self setMinimumHeight:viewBounds.size.height + detailHeight];
	[self setNeedsLayout:YES];
	
//	NSTextField *elementLabelTextField = (NSTextField *)[[self view] viewWithTag:ALTemplateEditorLabelTextFieldTag];
	ALElementBarTextFieldCell *elementLabelTextFieldCell = (ALElementBarTextFieldCell *)[[self elementLabel] cell];
	[elementLabelTextFieldCell setUseRoundedBottom:NO];
//	NSButton *addButton = (NSButton *)[[self view] viewWithTag:ALTemplateEditorAddButtonTag];
	ALElementBarAddButtonCell *buttonCell = (ALElementBarAddButtonCell *)[[self elementAddButton] cell];
	[buttonCell setUseRoundedBottom:NO];
	
	NSView *detailsInitialResponder = [detailsClone viewWithTag:ALTemplateEditorInitialResponderTag];
	[[self elementAddButton] setNextKeyView:detailsInitialResponder];
	NSView *detailsLastResponder = [detailsClone viewWithTag:ALTemplateEditorLastResponderTag];
	[detailsLastResponder setNextKeyView:[self elementLabel]];
	
	[[self elementLabel] setEditable:YES];
	[[[self view] window] makeFirstResponder:[self elementLabel]];
}

-(void)columnEntryDidEndEditing {
//	NSInteger tag = ALElementDetailsViewBaseTag + [[[self representedObject] elementType] integerValue];
//	NSView *detailsView = [[self view] viewWithTag:tag];
	[[self currentDetailView] removeFromSuperview];
	[self setMinimumHeight:0];
	[self setNeedsLayout:YES];
	
//	NSTextField *elementLabelTextField = (NSTextField *)[[self view] viewWithTag:ALTemplateEditorLabelTextFieldTag];
	[[self elementLabel] setEditable:NO];
	[[[self elementLabel] window] invalidateCursorRectsForView:[self elementLabel]];
	ALElementBarTextFieldCell *elementLabelTextFieldCell = (ALElementBarTextFieldCell *)[[self elementLabel] cell];
	[elementLabelTextFieldCell setUseRoundedBottom:YES];
//	NSButton *addButton = (NSButton *)[[self view] viewWithTag:ALTemplateEditorAddButtonTag];
	ALElementBarAddButtonCell *buttonCell = (ALElementBarAddButtonCell *)[[self elementAddButton] cell];
	[buttonCell setUseRoundedBottom:YES];
	
	NSResponder *firstResponder = [[[self view] window] firstResponder];
	if (![firstResponder isKindOfClass:[NSView class]] || ![(NSView *)firstResponder isDescendantOf:[self view]]) 
		return;
	[[[self view] window] makeFirstResponder:[self columnView]];
}


#pragma mark -
#pragma mark Accessors

+(NSSet *)keyPathsForValuesAffectingElementIsPrimary {
	NSString *lineNumberKey = [NSString stringWithFormat:@"%@.%@", ALRepresentedObjectKey, ALLineNumberKey];
	return [NSSet setWithObject:lineNumberKey];
}

-(BOOL)elementIsPrimary {
	if ([[[self representedObject] valueForKey:ALLineNumberKey] integerValue] == 0)
		return YES;
	return NO;
}


#pragma mark -
#pragma mark Text field delegate

-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command {
	if (command == @selector(insertNewline:)) {
		[[self columnView] stopEditing];
		return YES;
	}
	return NO;
}


#pragma mark -
#pragma mark Private

-(NSView *)detailViewForType_:(ALElementType)type {
	switch (type) {
		case ALElementTypeText:
			return [self textDetailView];
		case ALElementTypeRating:
			return [self ratingDetailView];
		default:
			break;
	}
	return nil;
}

@end
